﻿using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Reader.Libs.PDF.Helper;
using Reader.Libs.PRC;
using System;
using System.IO;
using System.Text;

namespace Reader.Libs.CONVERTER
{
    public class PdfConverter
    {
        public void ConvertToTxt(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                PdfReader reader = new PdfReader(SourcePath);
                SimpleTextExtractionStrategy S = new SimpleTextExtractionStrategy();
                string R = "";
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, S);
                    if (i == reader.NumberOfPages)
                        R = s;
                }

                System.IO.FileStream fs = new System.IO.FileStream(FileToSave, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(R);
                sw.Close();
                fs.Close();
            }
            catch { throw; } 
        }

        public void ConvertToDoc(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                PdfReader reader = new PdfReader(SourcePath);
                MyTextExtractionStategy S = new MyTextExtractionStategy();
                string R = "";
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, S);
                    if (i == reader.NumberOfPages)
                        R = s;
                }

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(R);
                sw.Close();
                fs.Close();

                objWord.Documents.Open(FileName: "C://tmp//tmp.html", Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocument97, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToDocX(string SourcePath, string FileToSave)
        {
            Application objWord = new Application();

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                if (!System.IO.Directory.Exists("C://tmp"))
                    System.IO.Directory.CreateDirectory("C://tmp");
                else
                {
                    foreach (FileInfo f in (new DirectoryInfo("C://tmp")).GetFiles())
                        f.Delete();
                    foreach (DirectoryInfo dir in (new DirectoryInfo("C://tmp")).GetDirectories())
                    {
                        dir.Delete(true);
                    }
                }

                PdfReader reader = new PdfReader(SourcePath);
                MyTextExtractionStategy S = new MyTextExtractionStategy();
                string R = "";
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, S);
                    if (i == reader.NumberOfPages)
                        R = s;
                }

                System.IO.FileStream fs = new System.IO.FileStream("C://tmp//tmp.html", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(R);
                sw.Close();
                fs.Close();

                objWord.Documents.Open(FileName: "C://tmp//tmp.html", Format: WdOpenFormat.wdOpenFormatWebPages);
                objWord.Visible = false;
                if (objWord.Documents.Count > 0)
                {
                    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                    oDoc.SaveAs(FileName: FileToSave, FileFormat: WdSaveFormat.wdFormatDocumentDefault, Encoding: MsoEncoding.msoEncodingUTF8);
                    ((_Document)oDoc).Close(SaveChanges: false);
                }
            }
            finally
            {
                ((_Application)objWord.Application).Quit(SaveChanges: false);
            }
        }

        public void ConvertToHtml(string SourcePath, string FileToSave)
        {
            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                PdfReader reader = new PdfReader(SourcePath);
                MyTextExtractionStategy S = new MyTextExtractionStategy();
                string R = "";
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, S);
                    if (i == reader.NumberOfPages)
                        R = s;
                }

                System.IO.FileStream fs = new System.IO.FileStream(FileToSave, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(R);
                sw.Close();
                fs.Close();
            }
            catch { throw; }
        }

        public void ConvertToMobi(string SourcePath, string FileToSave)
        {

            if (File.Exists(FileToSave))
            {
                File.Delete(FileToSave);
            }
            try
            {
                PdfReader reader = new PdfReader(SourcePath);
                MyTextExtractionStategy S = new MyTextExtractionStategy();
                string R = "";
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string s = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, S);
                    if (i == reader.NumberOfPages)
                        R = s;
                }

                MobiDoc md = new MobiDoc();
                md.BookContent = R;
                md.writeContent(FileToSave);
            }
            catch { throw; }
        }
    }
}
